using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Chat.Engine;
using IGlobalist.Chat.Engine.Common;

using System.Web.Script.Services;
using System.Web.Script;
using System.Web.Services;

namespace IGlobalist.Cms.Chat
{
    public partial class Public : ChatBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtMsg.Attributes.Add("onkeypress", "return APublic.clickButton(event,'btn')");
            lstMembers.Attributes.Add("ondblclick", "alert(this.selectedIndex)");

            if (!IsPostBack)
            {
                string roomID = Request.QueryString["rid"];
                hdnRoomID.Value = roomID;

                Room room = RoomConfig.GetChatRoom(roomID);
                if (Session[SessionVariable.Chat.CurrentAdmin + roomID] == null)
                    Response.Redirect("ChatLogin.aspx", true);

                ChatEngine.AddOrGetRoom(room);
                User user = new User(GetUserID(room.Administrator.UserName, room.ID), room.Administrator.UserName, UserType.Admin);
                user.Status = UserStatus.Online;
                string prevMsgs = "";
                room.JoinRoom(user);
                lblCurrentUser.Text = room.Administrator.UserName;
                lblRoomName.Text = room.Name;
                ltrMessage.Text = string.Format(ltrMessage.Text, prevMsgs);
                hdnCurrentAccount.Value = room.Administrator.UserName;

                this.Title = room.Administrator.UserName;
                foreach (string s in room.GetRoomUsersNames())
                {
                    lstMembers.Items.Add(new ListItem(s, s));
                }
            }
        }




        #region Script Callback functions

        /// <summary>
        /// This function is called from the client script 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="roomID"></param>
        /// <returns></returns>
        [WebMethod]
        static public Message[] SendMessage(string msg, string roomID, string receiver)
        {
            try
            {
                Room room = ChatEngine.GetRoom(roomID);
                Message[] res = null;
                if (room != null)
                {
                    res = room.SendMessage(msg, GetUserID(room.Administrator.UserName, room.ID), receiver, MsgType.Public);
                }
                return res;
            }
            catch (Exception ex)
            {

            }
            return new Message[0];
        }


        /// <summary>
        /// This function is called peridically called from the user to update the messages
        /// </summary>
        /// <param name="otherUserID"></param>
        /// <returns></returns>
        [WebMethod]
        static public Message[] UpdateUser(string roomID)
        {
            try
            {
                Room room = IGlobalist.Chat.Engine.ChatEngine.GetRoom(roomID);
                if (room != null)
                {

                    if (room != null)
                    {
                        return room.UpdateUser(GetUserID(room.Administrator.UserName, room.ID));
                    }

                }
            }
            catch (Exception ex)
            {

            }
            return new Message[0];
        }


        /// <summary>
        /// This function is called from the client when the user is about to leave the room
        /// </summary>
        /// <param name="otherUser"></param>
        /// <returns></returns>
        [WebMethod]
        static public string LeaveRoom(string roomID)
        {
            try
            {
                Room room = ChatEngine.GetRoom(roomID);
                if (room != null)
                {
                    room.Administrator.Status = UserStatus.IDE;
                    room.UpdateUser(room.Administrator);
                    //room.LeaveRoom(room.Administrator.ID);
                }
                //room.LeaveRoom(HttpContext.Current.Session["UserName"].ToString());
            }
            catch (Exception ex)
            {

            }
            return "";
        }


        /// <summary>
        /// Returns a comma separated string containing the names of the users currently online
        /// </summary>
        /// <param name="roomID"></param>
        /// <returns></returns>
        [WebMethod]
        static public string UpdateRoomMembers(string roomID)
        {
            try
            {
                Room room = IGlobalist.Chat.Engine.ChatEngine.GetRoom(roomID);
                if (room != null)
                {
                    IEnumerable<string> users = room.GetRoomUsersNames();
                    string res = "";

                    foreach (string s in users)
                    {
                        res += s + ",";
                    }
                    return res;
                }
            }
            catch (Exception ex)
            {
            }
            return "";
        }
        #endregion
    }
}
